package com.minidb.cli;

import com.minidb.sql_compliler.ASTNode;
import com.minidb.sql_compliler.Lexer;
import com.minidb.sql_compliler.Parser;

import java.util.Scanner;
import com.minidb.sql_compliler.*;
import com.minidb.engine.*;

public class Main {
//    public static void main(String[] args) {
//        Scanner sc = new Scanner(System.in);
//        System.out.println("MiniDB Started. Type 'quit' to exit.");
//        while (true) {
//            System.out.print("MiniDB > ");
//            String sql = sc.nextLine();
//            if (sql.equalsIgnoreCase("quit")) {
//                break;
//            }
//            // TODO: 调用 SQL 编译器和执行器
//            System.out.println("[DEBUG] Received SQL: " + sql);
//        }
//        sc.close();
//    }

//    public static void main(String[] args) {
//        Lexer lexer = new Lexer("CREATE TABLE users (id INT, name STRING)");
//        System.out.println(lexer.tokenize());
//    }
    public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      Executor executor = new Executor();
      System.out.println("MiniDB Started. Type 'quit' to exit.");

      while (true){
            System.out.print("MiniDB > ");
            String sql = sc.nextLine();
            if (sql.equalsIgnoreCase("quit"))break;

            try {
                Lexer lexer = new Lexer(sql); //词法分析
                Parser parser = new Parser(); //语法分析
                ASTNode ast = parser.parse(lexer.tokenize()); //生成AST
                String result = executor.execute(ast); //执行AST
                System.out.println(result); //打印结果
            } catch (Exception e) {
                System.out.println("Error: " + e.getMessage());
          }
      }
      sc.close();
    }
}
